const express = require('express');
const trainingMod= require('../module/training');
const util =require('../util');
const  trainingRouter= express.Router();
let status =1;
//添加训练
trainingRouter.post('',(req,res)=>{
     trainingMod.addTrining(req.body,(PK_TrainingGuid)=>{
            res.send({status,data:PK_TrainingGuid});
     })
})
//删除训练
trainingRouter.delete('',(req,res)=>{
    if(!req.query.id){
        res.send({status:0,msg:'请传入需要删除训练的Guid'});
        return
    }
    trainingMod.delTraining(req.query.id,(data)=>{
         res.send({status,data});
    })
})
//获取指定的训练和指定状态的训练
trainingRouter.get('',(req,res)=>{
    trainingMod.getTriningByStatus(req.query.id,req.query.status,(data)=>{
         res.send({status,data});
    }) 
})
//根据训练ID获取训练
trainingRouter.get('/id',(req,res)=>{
    if(!req.query.id){
        res.send({status:0,msg:'请传入需要获取训练的Guid'});
        return
    }
    trainingMod.getTraining(req.query.id,(data)=>{
        res.send({status,data});
    })
})
//根据创建者ID获取训练
trainingRouter.get('/creater',(req,res)=>{
    if(!req.query.id){
        res.send({status:0,msg:'请传入创建者的Guid'});
        return
    }
    trainingMod.getTriningOfCreater(req.query.id,req.query.status,(data)=>{
        res.send({status,data});
    })
})
//判断用户是否具有修改的权限
trainingRouter.post('/creater',(req,res)=>{
    let ids  = JSON.parse(req.body.ids);
     if(typeof[ids]!=='Array' && !ids[0]){
         res.send({status:0,msg:'请传入JSON格式的数组，并且不能为空数组'})
         return
     }else if(!req.body.userID){
         res.send({status:0,msg:'请传入用户Guid'})
         return
     }else{
         trainingMod.userAccredit(ids,req.body.userID,(data)=>{
             res.send({status,data});
         })
     }
})
//根据问卷ID和创建者获取训练
trainingRouter.get('/paper',(req,res)=>{
     if(!(req.query.id&&req.query.Creater)){
        res.send({status:0,msg:'请传入问卷的GUID和创建者的GUID'});
        return
    }
    trainingMod.getTriningOfPaper(req.query.id,req.query.status,req.query.Creater,(data)=>{
         res.send({status,data});
    })
})
// 根据ID恢复删除的训练
trainingRouter.get('/recover',(req,res)=>{
    if(!req.query.id){
         res.send({status:0,msg:'请传入需要恢复训练的Guid'});
        return
    }
    trainingMod.reCoverTrainingById(req.query.id,(data)=>{
        res.send({status,data});
    })
})
//修改训练
trainingRouter.put('',(req,res)=>{
     if(!req.body.id){
          res.send({status:0,msg:'请传入需要修改训练的Guid'});
        return
     }
     trainingMod.modifyTrainingById(req.body.id,req.body,(data)=>{
         res.send({status,data});
     })
})
trainingRouter.get('/class',(req,res)=>{
     if(!req.query.userID){
          res.send({status:0,msg:'请传入用户的Guid'});
        return;
     }
     trainingMod.getTrainingByClass(req.query.userID,req.query.classID,req.query.paperID,req.query.status,(data)=>{
         res.send({status,data});
     })
})
module.exports = trainingRouter;